<?php

namespace Caisse\CaisseBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Caisse\CaisseBundle\Entity\Commande;
use Caisse\CaisseBundle\Form\CommandeType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Caisse\CaisseBundle\Entity\ArticleCommandeRepository;

class ArticleCommandeController extends Controller {

    public function listCommandeMobileAction() {

        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $numTable = $request->get('numTable');
        $table = $em->getRepository('CaisseBundle:Tables')->find($numTable);
        $commandes = $em->getRepository("CaisseBundle:Commande")->findBy(array("table" => $table, 'is_payed' => false));
        $resultat = array();

        foreach ($commandes as $commande) {
            foreach ($commande->getLigneCommandes() as $ligneCommande) {
                $tab ['qte'] = $ligneCommande->getQtitee();
                $tab['PU'] = $ligneCommande->getPrixArticle();
                $tab['NomArticle'] = $ligneCommande->getArticle()->getNomArticle();
                $tab['Prix'] = $ligneCommande->getPrixArticle() * $ligneCommande->getQtitee();
                $resultat[] = $tab;
            }
        }
        $result['commande'] = $resultat;
        return new Response(json_encode($result));
    }

    public function validerPaymeentAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $numTable = $request->get('numTable');
        $table = $em->getRepository('CaisseBundle:Tables')->find($numTable);
        $table->setServeur(null);
        $commandes = $em->getRepository("CaisseBundle:Commande")->findBy(array("table" => $table, 'is_payed' => false));
        foreach ($commandes as $commande) {
            $commande->setIsPayed(1);
        }

        $em->flush();
        $resultat = array();
        $resultat['success'] = 'success';
        return new Response(json_encode($resultat));
    }

    public function ajoutCommandeMobileAction() {

        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $commande = $request->get('commande');
        $commande = json_decode($commande);
        $cmd = new Commande();
        $cmd->setDate(new \DateTime());
        $cmd->setPrixTotal($commande->total);
        $cmd->setTable($em->getRepository('CaisseBundle:Tables')->find($commande->table));

        $cmd->setServeur($em->getRepository('CaisseBundle:Serveur')->find($commande->serveur));
        $cmd->getTable()->setServeur($cmd->getServeur());
        $cmd->setNumCde(1);

        foreach ($commande->articles as $article) {
            $articleCommande = new \Caisse\CaisseBundle\Entity\ArticleCommande();
            $articl = $em->getRepository('CaisseBundle:Article')->find($article->id);
            $articleCommande->setArticle($articl);
            $articleCommande->setCommande($cmd);
            $articleCommande->setQtitee($article->qte);
            $articleCommande->setPrixArticle($articl->getPrix());
            $articleCommande->setNumTable($article->table);
            $em->persist($articleCommande);
        }
        $em->persist($cmd);
        $em->flush();
        $resultat = array();
        $resultat['success'] = 'success';
        return new Response(json_encode($resultat));
    }

    public function AjoutCommandeAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $id = $request->get('id');
        if ($id != '') {
            $commande = $em->getRepository('CaisseBundle:Commande')->find($id);
        } else
            $commande = new Commande();
        $commande->setDate(new \DateTime());
        $form = $this->createForm(new CommandeType(), $commande);

        if ($request->isMethod('Post')) {
            $commande->bind($request);
            if ($form->isValid()) {

                $commande = $form->getData();
                $repository = $em->getRepository('CaisseBundle:Commande');
                $em->persist($commande);
                $em->flush();
                $commandes = $repository->findAll();
                return $this->render('CaisseBundle:Commande:listCommande.html.twig', array('commandes' => $commandes));
            }
        }
        return $this->render('CaisseBundle:Commande:AjoutCommande.html.twig', array('form' => $form->createView(), 'id' => $id));
    }

}
